﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BCGC.BLL.Helper;
using BCGC.BLL.BusinessObject.UserAccount;
using Jalal.Library.Web;
using System.IO;
using Jalal.Library.Utility;
using System.Collections.Generic;

public partial class Member_Profile : MemberBasePage
{
    private SiteUser CurrentUser
    {
        get
        {
            return SiteUser.GetUserById(SessionManager.LoggedUser.Id, true);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitControls();
            LoadData();
        }
    }

    private void InitControls()
    {
        //btnProfileUpload.Attributes.Add("onclick", "javascript:hideProfilePopup()");
    }

    private void LoadData()
    {
        SiteUser user = CurrentUser;

        if (user != null)
        {
            if (File.Exists(Server.MapPath(user.Profile.RealImageUrl)))
                imgProfileImage.ImageUrl = user.Profile.RealImageUrl;
            else
                imgProfileImage.ImageUrl = ThemeFolder + "/image/no_profile_image.jpg";

            tbxFavoriteGames.Text = user.Profile.FavoriteGames;
            tbxAboutMe.Text = user.Profile.UserBio;
            lblMemberId.Text = user.Profile.MemberId;
            lblUsername.Text = user.Username;
            tbxRealName.Text = user.Profile.RealName;
            tbxNickName.Text = user.Profile.NickName;
            tbxDateOfBirth.Text = CommonUtility.FormatedDateString(user.Profile.DateOfBirth);
            tbxMessenger.Text = user.Profile.Messenger;
            tbxFacebookProfile.Text = user.Profile.FacebookProfileUrl;
            tbxCity.Text = user.Profile.City;
            ddlPlayerStyle.SelectedValue = user.Profile.PlayerStyle;
            SetElement(user.Profile.Element);
            tbxMode.Text = user.Profile.Mood;

            if (File.Exists(Server.MapPath(user.Profile.AvatarImageUrl)))
                imgAvatar.ImageUrl = user.Profile.AvatarImageUrl;
            else
                imgAvatar.ImageUrl = ThemeFolder + "/image/no_avatar_image.jpg";

            rblImageSelection.SelectedValue = user.Profile.UseProfileImage.ToString();
        }
    }

    protected void Save_Click(object sender, ImageClickEventArgs e)
    {
        if (IsValid)
        {
            UserProfile profile = new UserProfile();

            profile.FavoriteGames = tbxFavoriteGames.Text;
            profile.UserBio = tbxAboutMe.Text;
            profile.RealName = tbxRealName.Text;
            profile.NickName = tbxNickName.Text;
            profile.DateOfBirth = DateTime.Parse(tbxDateOfBirth.Text);
            profile.Messenger = tbxMessenger.Text;
            profile.FacebookProfileUrl = tbxFacebookProfile.Text;
            profile.City = tbxCity.Text;
            profile.PlayerStyle = ddlPlayerStyle.SelectedValue;
            profile.Element = GetElement();
            profile.Mood = tbxMode.Text;
            profile.UseProfileImage = bool.Parse(rblImageSelection.SelectedValue);

            if (profile.Update())
                uccResponse.SetSuccessMessage("Profile updated successfully");
            else
                uccResponse.SetFailMessage("Failed to save information, please try again");
        }
        else
            uccResponse.SetFailMessage("Failed to save information, please try again");
    }

    private string GetElement()
    {
        if (rbtFire.Checked)
            return "Fire";
        else if (rbtLife.Checked)
            return "Life";
        else if (rbtSpirit.Checked)
            return "Spirit";
        else if (rbtWater.Checked)
            return "Water";
        else if (rbtEarth.Checked)
            return "Earth";
        else if (rbtWind.Checked)
            return "Wind";
        else
            return null;
    }

    private void SetElement(string elementValue)
    {
        rbtFire.Checked = rbtWind.Checked = rbtWater.Checked = 
            rbtLife.Checked = rbtEarth.Checked = rbtSpirit.Checked = false;

        if (string.Compare(elementValue, "Fire", true) == 0)
            rbtFire.Checked = true;
        else if (string.Compare(elementValue, "Wind", true) == 0)
            rbtWind.Checked = true;
        else if (string.Compare(elementValue, "Water", true) == 0)
            rbtWater.Checked = true;
        else if (string.Compare(elementValue, "Life", true) == 0)
            rbtLife.Checked = true;
        else if (string.Compare(elementValue, "Earth", true) == 0)
            rbtEarth.Checked = true;
        else if (string.Compare(elementValue, "Spirit", true) == 0)
            rbtSpirit.Checked = true;
    }

    protected void UploadProfileImage(object sender, EventArgs e)
    {
        
        List<string> paths = uccProfileImageUploader.UploadImage();
    }
}
